
package com.utilstest.jsonutils;

import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.utils.jsonutils.JSONDeserializer;
import com.utils.jsonutils.JSONSerializer;
import com.utilstest.jsonutils.beans.Account;

public class JsonUtilsTestActivity extends Activity implements OnClickListener {

	private final String TEST_JSON_FILE_NAME = "jsondata.json";

	private TextView resultView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findViewById(R.id.serializer).setOnClickListener(this);
		findViewById(R.id.deserializer).setOnClickListener(this);

		resultView = (TextView) findViewById(R.id.result);
	}

	@Override
	public void onClick(View v) {

		try{

			InputStream jsonContentStream = getAssets().open(TEST_JSON_FILE_NAME);

			Account test = JSONDeserializer.getInstance().deserialize(Account.class, jsonContentStream);

			switch(v.getId()) {
				case R.id.serializer:
					String jsonContent = JSONSerializer.getInstance().serializer(test);
					resultView.setText(jsonContent);
					break;
				case R.id.deserializer:
					resultView.setText(test.toString() + " created");
					break;
			}
		} catch(Exception e){
			resultView.setText("Exception : " + e.getMessage());
		}
	}
}
